package com.zlp.controller;

import com.alibaba.fastjson.JSON;
import com.zlp.entity.Employee;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Classname MainTest
 * @Description TODO
 * @Date 2022/10/12 13:45
 * @Created by ZouLiPing
 */
public class MainTest {

    public static void main(String[] args) {
        //员工列表
        List<Employee> employList = new ArrayList<Employee>();
//添加员工
        employList.add(new Employee(20200401, "Mike", 25));
        employList.add(new Employee(20200402, "Mary", 26));
        employList.add(new Employee(20200403, "Jack", 28));
        employList.add(new Employee(20200404, "Tom", 23));
        employList.add(new Employee(20200405, "Lucy", 21));
        employList.add(new Employee(20200406, "Jim", 26));
        employList.add(new Employee(20200407, "David", 29));
        employList.add(new Employee(20200408, "Jack", 22));

/**
 * 将员工列表规约处理为姓名和年龄集合，由于员工有重名，符合规约异常问题现象复现
 */
//异常规约处理方式，不进行出现重名时的处理---出现规约冲突异常；
//        Map<String, Integer> nameAgeIgnoreRepeatMap = employList.stream().collect(Collectors.toMap(Employee :: getUsername, Employee :: getAge));
//对重名现象进行处理，避免规约冲突；“(v1, v2) -> v1”，当出现冲突时默认取初始出现值；实际中可以根据自己的实际情况取值
        Map<String, Integer> nameAgeMap = employList.stream().collect(Collectors.toMap(Employee :: getUsername, Employee :: getAge, (v1, v2) -> v1));
        Map<String, Employee> map = employList.stream().collect(Collectors.toMap(Employee :: getUsername, refundOrderDO -> refundOrderDO, (refundOrderDO1, refundOrderDO2) -> refundOrderDO1));
        System.out.println(JSON.toJSONString(nameAgeMap));

        Map<String, Employee> map2 = employList.stream().collect(Collectors.toMap(Employee::getUsername, refundOrderDO -> refundOrderDO));

        for (Map.Entry<String, Employee> stringEmployeeEntry : map.entrySet()) {
            System.out.println(stringEmployeeEntry.getValue());
        }

    }
}
